﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Common.Utilities;

namespace Workflows.Components.Entities.Distances.FieldDistances
{
    public class FuzzyStringComparer:IAttributeDistanceCalcutor
    {
        #region Implementation of IAttributeComparer

        /// <summary>
        /// load dictionary into memory or read config settings from file
        /// </summary>
        public void InitInMemorySettings()
        {
            
        }

        /// <summary>
        /// return similarity score in [0,1]
        /// 0 indicate no similarity or null match
        /// 1 indicate perfect match
        /// </summary>
        /// <param name="attrValue1"></param>
        /// <param name="attrValue2"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public double Compare(object attrValue1, object attrValue2, ref MatchResult result)
        {
            if (attrValue1 == null || attrValue2 == null)
                return 0;
            string str1 = attrValue1.ToString().Trim();
            string str2 = attrValue2.ToString().Trim();
            if (str1.Length == 0 || str2.Length == 0)
                return 0;

            double score = 0;
            if (str1.ToLower() == str2.ToLower())
            {
                result = MatchResult.PerfectMatch;
                score = 1.0;
            }
            else
            {
                int subSeq = StringUtil.LongestCommonSubsequence(str1, str2);
                int denominator = Math.Min(str1.Length, str2.Length);
                score = (double) subSeq/denominator;
                result = MatchResult.PositiveMatch;
            }
            return score;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity1"></param>
        /// <param name="entity2"></param>
        /// <param name="attrMetaData"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public double Compare(Entity entity1, Entity entity2, 
            AttributeMetaData attrMetaData, ref MatchResult result)
        {
            Debug.Assert(!string.IsNullOrEmpty(attrMetaData.AttributeName));

            string attrName = attrMetaData.AttributeName;
            object attrValue1 = null;
            object attrValue2 = null;
            if (entity1.ComparableAttributes.ContainsKey(attrName))
                attrValue1 = entity1.ComparableAttributes[attrName];
            if (entity2.ComparableAttributes.ContainsKey(attrName))
                attrValue2 = entity2.ComparableAttributes[attrName];
            return this.Compare(attrValue1, attrValue2, ref result);
        }

        /// <summary>
        /// argument to bind attribute name
        /// </summary>
        public string[] ArgumentNames
        {
            get
            {
                return new string[] { "Name" };
            }
        }

        public Dictionary<string, string> ComparerSettings
        {
            get { return new Dictionary<string, string>(); }
            set { }
        }

        #endregion
    }
}
